//
//  ProductListViewController.m
//  Castlery
//
//  Created by xie weizhi on 8/8/17.
//  Copyright © 2017 xie weizhi. All rights reserved.
//

#import "ProductListViewController.h"
// Models
#import "ProductListDataModel.h"
// Views
#import "ProductListTableViewCell.h"
// Controllers
#import "ProductDetailViewController.h"
// Others
#import "ArrayDataSource.h"
#import "UIScrollView+Refresh.h"

static NSString *const ProductListTableViewCellID = @"cellID";
static CGFloat const ProductListTableViewCellHeight = 300.f;

@interface ProductListViewController () <UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ArrayDataSource *dataSource;
@property (nonatomic, strong) ProductListDataModel *dataModel;
@end

@implementation ProductListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Castlery";
    
    [self setupTableView];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - Private

- (void)setupTableView {
    [self.dataSource setCellCreationBlock:^id(id parentView, id item, NSIndexPath *indexPath) {
        UITableView *tableView = parentView;
        return [tableView dequeueReusableCellWithIdentifier:ProductListTableViewCellID forIndexPath:indexPath];
    }];
    
    [self.dataSource setCellConfigureBlock:^(id parentView, id aCell, id item, NSIndexPath *indexPath) {
        ProductListTableViewCell *cell = aCell;
        [cell configureCellWithProduct:item];
    }];
    
    [self.view addSubview:self.tableView];

    [self.tableView addRefreshHeaderWithTarget:self selector:@selector(pullToRefreshHeaderAction:)];
    [self.tableView addRefreshFooterWithTarget:self selector:@selector(pullToRefreshFooterAction:)];
}

- (void)requestDataWithPage:(NSInteger)page {
    __weak typeof(self) weakSelf = self;
    [self.dataModel requestProductListWithPage:page completion:^(NSError *error, NSArray<Product *> *productList) {
        
        __strong typeof(weakSelf) strongSelf = weakSelf;
        
        if (error) {
            return;
        }
        
        if (0 == page) {
            // reload
            [strongSelf.tableView.mj_header endRefreshing];
            strongSelf.dataSource.items = productList;
        } else {
            // append another page
            [strongSelf.tableView.mj_footer endRefreshing];
            [strongSelf.dataSource appendItems:productList];
        }
        
        [strongSelf.tableView reloadData];
    }];
}

#pragma mark - UI Action

- (void)pullToRefreshHeaderAction:(id)sender {
    
    [self requestDataWithPage:0];
}

- (void)pullToRefreshFooterAction:(id)sender {
    NSInteger nextPage = self.dataModel.currentPage + 1;
    [self requestDataWithPage:nextPage];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    Product *selectedProduct = [self.dataSource itemAtIndexPath:indexPath];
    ProductDetailViewController *detailVC = [ProductDetailViewController new];
    detailVC.productID = selectedProduct.productID;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ProductListTableViewCellHeight;
}

#pragma mark - Accessors

- (ProductListDataModel *)dataModel {
    if (!_dataModel) {
        _dataModel = [ProductListDataModel new];
    }
    
    return _dataModel;
}

- (ArrayDataSource *)dataSource {
    if (!_dataSource) {
        _dataSource = [ArrayDataSource new];
        _dataSource.items = nil;
    }
    
    return _dataSource;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.delegate = self;
        _tableView.dataSource = self.dataSource;
        _tableView.frame = self.view.bounds;
        [_tableView registerClass:[ProductListTableViewCell class] forCellReuseIdentifier:ProductListTableViewCellID];
    }
    
    return _tableView;
}


@end
